/* eslint-disable vue/no-unused-vars */
<template>
  <el-table
    type="index"
    :data="tableData.filter(data => !search || data.itemName.toLowerCase().includes(search.toLowerCase()))"
    style="width: 100%;font-size: 20px;"
    >
    <el-table-column
      label="订单时间"
      prop="date">
    </el-table-column>
    <el-table-column
      label="物品名称"
      prop="itemName">
    </el-table-column>
    <el-table-column
      label="借入者账号"
      prop="borrow_id">
    </el-table-column>
    <el-table-column
      label="交易金额"
      prop="price">
    </el-table-column>
    <el-table-column
      label="租借时长"
      prop="rentalTime">
    </el-table-column>
    <el-table-column
      label="订单状态"
      prop="state">
    </el-table-column>
    
    <!-- 搜索框 -->
    <el-table-column
      align="right">
      // eslint-disable-next-line vue/no-unused-vars
      <template slot="header">
        <el-input
          v-model="search"
          size="small"
          placeholder="输入关键字搜索"/>
      </template>
      <!-- 按钮 -->
      <template slot-scope="scope" >
        <el-button 
          type="success"
          size="small"
          @click="agree(scope.$index)">同意</el-button>
        <el-button
          type="danger"
          size="small"
          @click="refuse(scope.$index)">拒绝</el-button>
          <el-button
          type="primary"
          size="small"
          @click="okk(scope.$index)">完成订单</el-button>
      </template>
    </el-table-column>
  </el-table>
</template>

<script>
  export default {
    created(){
      var self = this;
      let config = {
          headers: {'Content-Type': 'application/json','Authorization':localStorage.getItem("token")}
        }
      var id = localStorage.getItem("id");
      this.$axios
      .get('/transaction/byLender_id?lender_id=' + id,config)
      .then(function (response) {
        var array = response.data
        // handle success
        console.log(response);
        console.log(array);
        for (var i=0;i<array.length;i++){ 
          // 把状态码装换成中文状态描述
          var stateCode = array[i].state
          var stateDesc = ''
          if(stateCode == 0){
            stateDesc = '待申请'
          }
          if(stateCode == -1){
            stateDesc = '申请被拒绝'
          }
          if(stateCode == 1){
            stateDesc = '同意申请'
          }
          if(stateCode == 2){
            stateDesc = '交易完成'
          }
          let item = {
            date:array[i].time,
            price:array[i].amount,
            borrow_id:array[i].borrow_id,
            rentalTime:array[i].rentalTime + '日',
            state:stateDesc,
            id:array[i].id,
            borrow_id:array[i].borrow_id,
            itemName:array[i].itemName
          }
          self.tableData.push(item)
        }
      })
      .catch(function (error) {
        // handle error
        console.log(error);
      })
      .then(function () {
      // always executed
     });
    },
    
    methods: {
      handleClick(row) {
        console.log(row);
      },
      // 接受订单
      agree(index) {
        console.log(index)
        var self = this
        var id = this.tableData[index].id
        console.log(id)

        let config = {
          headers: {'Content-Type': 'application/json','Authorization':localStorage.getItem("token")}
        }
        
        console.log(config)

        var uurl = '/updateTransState?id=' + id + '&state=1';
        // var id = localStorage.getItem("id");
        let param = {}
        this.$axios.put(uurl,param,config)
        .then(function (response) {
        
        })
        .catch(function (error) {
          // handle error
        })
        .then(function () {
          // always executed
        });
      },
      // 拒绝订单
      refuse(index) {
        var self = this
        var id = this.tableData[index].id
        console.log(id)
        let config = {
          headers: {'Content-Type': 'application/json','Authorization':localStorage.getItem("token")}
        }
        // var id = localStorage.getItem("id");
        let param = {}
        this.$axios
        .post('/updateTransState?id=' + id + '&state=-1',param,config)
        .then(function (response) {
        
        })
        .catch(function (error) {
          // handle error
        })
        .then(function () {
          // always executed
        });
      },
      // 完成订单
      okk(index) {
        var self = this
        var id = this.tableData[index].id
        console.log(id)
        let config = {
          headers: {'Content-Type': 'application/json','Authorization':localStorage.getItem("token")}
        }
        // var id = localStorage.getItem("id");
        let param = {}
        this.$axios
        .post('/updateTransState?id=' + id + '&state=2',param,config)
        .then(function (response) {
        
        })
        .catch(function (error) {
          // handle error
        })
        .then(function () {
          // always executed
        });
      }
    },

    data() {
      return {
        tableData: [
        ],
        id:[],
        search: ''
      }
    },
  }
</script>